package com.itheima.controller;

import com.itheima.anno.OperateLog;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //分页查询
    @GetMapping("/clazzs")
    public Result findAll(ClazzQueryParam clazzQueryParam){
        PageResult pageResult = clazzService.findAll(clazzQueryParam);
        return Result.success(pageResult);
    }

    @OperateLog
    //删除班级
    @DeleteMapping("/clazzs/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    @OperateLog
    //添加班级
    @PostMapping("/clazzs")
    public Result insertClazz(@RequestBody Clazz clazz){
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/clazzs/{id}")
    public Result findById(@PathVariable("id") Integer id){
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    @OperateLog
    //修改班级
    @PutMapping("/clazzs")
    public Result update(@RequestBody Clazz c){
        clazzService.update(c);
        return Result.success();
    }

    //查询所有班级
    @GetMapping("/clazzs/list")
    public Result list(){
        List<Clazz> list = clazzService.list();
        return Result.success(list);
    }
}
